package edu.gui;
import edu.Dijkstra;
import edu.MapNode;
import edu.MapWay;
import edu.WeightedGraph;
import edu.XMLRouteHandler;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
*
* @author Nikolaj Schepsen
*/
public class MapGUI extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
ArrayList<MapNode> NodeList;
ArrayList<MapWay> WayList;
// Anzahl von Klicks
private int clickNumber = 0;
private WeightedGraph G;
private MapNode startNode;
private MapNode zielNode;
// Wenn zwei Punkte nah liegen, es wird nur einen ausgewählt
private boolean isStartChoosed = false;
private boolean isZielChoosed = false;
// Die kürzeste Strecke
ArrayList<MapNode> shortWay;
/** Creates new form MapGUI */
public MapGUI() {
initComponents();
initParser();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed"
// desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
new edu.gui.LandKarte();
jScrollPane1 = new javax.swing.JScrollPane();
landKarte2 = new edu.gui.LandKarte();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
landKarte2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
landKarte2MouseClicked(evt);
}
});
javax.swing.GroupLayout landKarte2Layout = new javax.swing.GroupLayout(landKarte2);
landKarte2.setLayout(landKarte2Layout);
landKarte2Layout.setHorizontalGroup(landKarte2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 756, Short.MAX_VALUE));
landKarte2Layout.setVerticalGroup(landKarte2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 998, Short.MAX_VALUE));
jScrollPane1.setViewportView(landKarte2);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 773, Short.MAX_VALUE).addContainerGap()));
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE).addContainerGap()));
pack();
}// </editor-fold>//GEN-END:initComponents
private void landKarte2MouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_landKarte2MouseClicked
switch (clickNumber) {
case 0:
for (int i = 0; i < NodeList.size(); i++) {
if (getDistanz(evt.getX(), evt.getY(), (int) NodeList.get(i).getX(), (int) NodeList.get(i).getY()) < 15 && !isStartChoosed) {
landKarte2.selectedItems.add(new Ellipse2D.Float(NodeList.get(i).getX() - 10, NodeList.get(i).getY() - 10, 20, 20));
startNode = NodeList.get(i);
//System.out.println("Clicked 1-st time");
clickNumber += 1;
isStartChoosed = true;
repaint();
}
}
break;
case 1:
for (int i = 0; i < NodeList.size(); i++) {
if (getDistanz(evt.getX(), evt.getY(), (int) NodeList.get(i).getX(), (int) NodeList.get(i).getY()) < 15 && !isZielChoosed) {
landKarte2.selectedItems.add(new Ellipse2D.Float(NodeList.get(i).getX() - 10, NodeList.get(i).getY() - 10, 20, 20));
zielNode = NodeList.get(i);
//System.out.println("Clicked 2-nd time");
clickNumber += 1;
isZielChoosed = true;
repaint();
}
}
break;
case 2:
drawlines(G);
clickNumber += 1;
repaint();
break;
default:
clickNumber = 0;
landKarte2.selectedItems.clear();
//System.out.println(landKarte2.selectedItems.size());
landKarte2.lines.clear();
//System.out.println(landKarte2.lines.size());
shortWay.clear();
//System.out.println(shortWay.size());
isStartChoosed = false;
isZielChoosed = false;
repaint();
break;
}
}// GEN-LAST:event_landKarte2MouseClicked
public int getDistanz(int x1, int y1, int x2, int y2) {
return (int) Math.sqrt(Math.pow(x2 - x1, 2.0) + Math.pow(y2 - y1, 2.0));
}
/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
// <editor-fold defaultstate="collapsed"
// desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase
* /tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MapGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MapGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MapGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MapGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
// </editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MapGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private edu.gui.LandKarte landKarte2;
// End of variables declaration//GEN-END:variables
private void initParser() {
// Initialisiere den Parser
SAXParserFactory factory = SAXParserFactory.newInstance();
XMLRouteHandler handler = new XMLRouteHandler();
// try{"lese die xml-Datei ein"} catch{Exception}
try {
SAXParser file = factory.newSAXParser();
file.parse("src/edu/xml/route2.xml", handler);
} catch (Exception e) {
e.printStackTrace();
}
// TODO: muss durch die unterzufinden Elementen ersetzt werden
//pNDS = handler.getMapNodeList();
// Lese alle Wege/Städte aus der xml-Datei ein
WayList = handler.getMapWayList();
NodeList = handler.getMapNodeList();
// Erstelle einen Graph G
G = new WeightedGraph(NodeList.size());
// Befülle den Graph mit Knoten
for (int i = 0; i < G.getNumVertices(); i++) {
G.setStadt(i, NodeList.get(i));
}
// Befülle den Graph mit Kanten
for (int i = 0; i < WayList.size(); i++) {
for (int j = 0; j < WayList.get(i).getNd().size() - 1; j++) {
int refS = WayList.get(i).getNd().get(j);
int refE = WayList.get(i).getNd().get(j + 1);
int x1 = (int) G.getStadt(G.getIndex(refS)).getX();
int y1 = (int) G.getStadt(G.getIndex(refS)).getY();
int x2 = (int) G.getStadt(G.getIndex(refE)).getX();
int y2 = (int) G.getStadt(G.getIndex(refE)).getY();
int weight = (int) Math.sqrt(Math.pow(x2 - x1, 2.0) + Math.pow(y2 - y1, 2.0));
G.addEdge(G.getIndex(refS), G.getIndex(refE), weight);
}
} // Nun ist der Graph betriebsbereit!
}
private void drawlines(WeightedGraph G) {
shortWay = Dijkstra.shortestPath(G, G.getIndex(startNode.getId()), G.getIndex(zielNode.getId()));
for (int n = 0; n < shortWay.size() - 1; n++) {
landKarte2.lines.add(new Line2D.Float(shortWay.get(n).getX(), shortWay.get(n).getY(), shortWay.get(n + 1).getX(), shortWay.get(n + 1).getY()));
}
}
}